//商品列表页面
import 'package:flutter/material.dart';
import 'package:test_xzkdemo/utils/weights/Refresh.dart';
import 'package:test_xzkdemo/utils/common/global.dart';

//列表页面示例
class GoodsListPage extends StatefulWidget {
  final String title = "商品列表页";

  GoodsListPage({Key key, String title}) : super(key: key);

  @override
  _GoodsListPageState createState() => new _GoodsListPageState();
}

class _GoodsListPageState extends State<GoodsListPage>
    with SingleTickerProviderStateMixin {
  int _counter = 10;

  Future<Null> getData() {
    setState(() {
      _counter += 10;
      if (_counter > 40) {
        isnomore = true;
      }
    });
    return Future.delayed(Duration(milliseconds: 500), () {});
  }

  Future<Null> reset() {
    setState(() {
      list.clear();
      _counter = 20;
      isnomore = false;
    });
    return Future.delayed(Duration(milliseconds: 500), () {});
  }

  @override
  void initState() {
    super.initState();
  }

  Key key = GlobalKey();
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        body: Center(
      child: new Expanded(
          child: RefreshLayout(
              canloading: !isnomore,
              onRefresh: (boo) {
                if (!boo) {
                  return getData();
                } else {
                  return reset();
                }
              },
              key: key,
              child: new ListView(
                children: _listBuilder(_counter),
              ),
              loadingBuilder: (BuildContext context) {
                return Padding(
                  padding: EdgeInsets.all(15.0),
                  child: Text('正在加载中...'),
                );
              })),
    ));
  }

  ///goodsdetail
  bool isnomore = false;

  final List<String> list = [];

  List<Widget> _listBuilder(int i) {
    final List<Widget> listw = [];
    if (list.isEmpty) {
      for (int i = 0; i < 1000; i++) {
        list.add("acs" + i.toString());
      }
    }
    for (int count = 0; count < i; count++) {
      listw.add(
        new GestureDetector(
          onTap: ()=>_onListItem(i,list[count]),
            child: ListTile(
          leading: Image.network(
            'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2008531081,2093585221&fm=11&gp=0.jpg',
            fit: BoxFit.cover,
          ),
          title: Text(list[count]),
          subtitle: Text(list[count]),
        )),
      );
    }
    listw.add(
      Padding(
          padding: EdgeInsets.only(bottom: 16.0),
          child: Center(child: Text(!isnomore ? "" : '我是有底线的'))),
    );
    return listw;
  }
//item进行点击的点击事件
  void _onListItem(int position,String msg){
    G.showToastBottom(msg);
    Navigator.of(context).pushNamed("/goodsdetail");
  }
}
